package common

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
)

// EncryptionPassword 用户密码加密
func EncryptionPassword(password, salt string) (string, error) {
	e, err := Md5([]byte(password))
	if err != nil {
		return "", err
	}
	ee, err := Md5([]byte(fmt.Sprintf("%suser%s", e, salt)))
	if err != nil {
		return "", err
	}
	return ee, nil
}

// Md5 加密
func Md5(str []byte) (string, error) {
	m := md5.New()
	_, err := m.Write(str)
	if err != nil {
		return "", err
	}
	e := m.Sum(nil)
	return hex.EncodeToString(e), nil
}

// Md5IgnoreError 忽略错误 md5加密
func Md5IgnoreError(str []byte) string {
	m := md5.New()
	m.Write(str)
	return hex.EncodeToString(m.Sum(nil))
}
